package cn.tedu.demo.controller;

import cn.tedu.demo.common.Result;
import cn.tedu.demo.entity.Book;
import cn.tedu.demo.service.BookService;
import cn.tedu.demo.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping
    public Result<?> save(@RequestBody Book book) {
        bookService.save(book);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Book book) {
        bookService.update(book);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> checkBook(@PathVariable Integer id){
        Book Book=bookService.checkBook(id);
        return Result.success(Book);
    }


    @GetMapping("/list")
    public Result<?> getByPage(PageInfo pageInfo) {
        pageInfo = bookService.getByPage(pageInfo);
        return Result.success(pageInfo);
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id) {
        bookService.delete(id);
        return Result.success();
    }


    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids){
        bookService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/getByUserid/{u_id}")
    public Result<?> getByUserid(@PathVariable("u_id")Integer id){
        List<Book> bookList=bookService.getByUserid(id);
        return Result.success(bookList);
    }



}
